<?php
/*
 * @Date: 2020-05-03 22:03:52
 * @名称: 激活控制器
 * @版本: 0.01
 * @作者: 初雪桜
 * @邮箱: 202184199@qq.com
 * @最后编辑人: 初雪桜
 * @LastEditTime: 2021-02-28 23:01:14
 * @FilePath: /Site/app/useradmin/controller/ActivateUser.php
 */

namespace app\useradmin\controller;

use app\BaseController;
use app\useradmin\controller\DataBaseController\AuthorAndChineseTeam;
use app\useradmin\controller\DataBaseController\ComicsAddInvitationAact;
use think\facade\Db;

class ActivateUser extends BaseController
{
    public function AuthorAndChineseTeam()
    {

        $authcodeInfo = strtr(input("authcodeInfo"), '-_', '+/'); //被加密的数据
        $private_key = input("private_key"); //私钥
        $codeLostTime = date("Y-m-d", time() - config("useradminConfig.ValidTimeOfActivationFile")); //链接过期时间

        if ($private_key == null || $authcodeInfo == null) {
            return view("OtherPage/Error", [
                "errorCode" => "ActivateUser-10000",
                "errorTitle" => "发生意外错误",
                "errorMsg" => "发生意外错误",
            ]);
        }
        $authcodeInfo = base64_decode($authcodeInfo); //吧加密数据解密
        $parameter = authcode($authcodeInfo, "DECODE", $private_key);
        $UnParameter = json_decode($parameter, true);

        if (strtotime($codeLostTime) > strtotime($UnParameter['Y-m-d'])) { //判断url传入的申请时间过期
            return view("OtherPage/Error", [
                "errorCode" => "ActivateUser-10001",
                "errorTitle" => "发生意外错误",
                "errorMsg" => "您的验证链接已过期 请联系作者重新邀请您的加入",
            ]);
        }
        $ComicsAddInvitationAact  = new ComicsAddInvitationAact($this->app); //查询是否过期
        $youIsExistence = $ComicsAddInvitationAact->find( //只会查询两天内的
            $UnParameter["user_id"],
            $UnParameter["book_id"],
            [
                "id",
                "comics_id", //书籍id
                "user_id", //用户id
                "private_key", //私钥
                "add_time", //添加时间
                "certification_time", //认证时间
                "aact_id", //author_and_chinese_team 表中的id
            ],
        )['data'];
        if ($youIsExistence) { //查询两天内是否有数据
            if (strtotime($UnParameter['Y-m-d']) != strtotime($youIsExistence['add_time'])) { //判断验证码是否过期
                return view("OtherPage/Error", [
                    "errorCode" => "ActivateUser-10003",
                    "errorTitle" => "发生意外错误",
                    "errorMsg" => "您的链接过期时间 解析时间 与数据库时间不匹配",
                ]);
            } else {
                if ($youIsExistence['certification_time'] != null) { //判断是否已认证
                    return view("OtherPage/Error", [
                        "errorCode" => "ActivateUser-10002",
                        "errorTitle" => "发生意外错误",
                        "errorMsg" => "您的链接已于{$youIsExistence['certification_time']}认证完毕 请直接操作此漫画",
                    ]);
                }
            }
        } else { //最近两天内没有数据 要么假造的要么申请时间过期了
            return view("OtherPage/Error", [
                "errorCode" => "ActivateUser-10001",
                "errorTitle" => "发生意外错误",
                "errorMsg" => "您的验证链接已过期 请联系作者重新邀请您的加入",
            ]);
        }
        $returnData = $ComicsAddInvitationAact->update(
            $youIsExistence['id'],
            $youIsExistence['comics_id'],
            $youIsExistence['user_id'],
            $youIsExistence['private_key'],
            [
                "certification_time" => date("Y-m-d")
            ]
        );
        if ($returnData['code'] != 200) { //判断是否未成功更新数据
            return view("OtherPage/Error", [
                "errorCode" => "ActivateUser-10004",
                "errorTitle" => "发生意外错误",
                "errorMsg" => "链接审核成功 操作数据库失败 请重新获取数据 或联系管理员 ",
            ]);
        }
        //成功了更新 
        $AuthorAndChineseTeam = new AuthorAndChineseTeam($this->app);
        $AuthorAndChineseTeam->Update(
            $youIsExistence['user_id'],
            $youIsExistence['comics_id'],
            $youIsExistence['aact_id'],
            [
                "state" => 0,
                "exp_state" => null
            ]
        );
    }
}
